//
//  SourceSubViewController.swift
//  clreader
//
//  Created by colin on 2019/4/1.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class SourceSubViewController: ScrollViewController {
    
    private var infoData:JSONObject?
    private lazy var infoTableView = UITableView(frame: CGRect.zero, style: .plain)
    private var headerHeight:CGFloat = 0

    convenience init(data:JSONObject?,headerHeight:CGFloat) {
        self.init()
        self.infoData = data
        self.headerHeight = headerHeight
    }
    
    override func scrollView() -> UIScrollView? {
        return self.infoTableView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        self.infoTableView.do {
            $0.showsVerticalScrollIndicator = false
            $0.sectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.sectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.estimatedSectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
            $0.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
            $0.contentInsetAdjustmentBehavior = .never
            $0.register(BookCell.self, forCellReuseIdentifier: BookCell.reuseId)
            $0.delegate = self
            $0.dataSource = self
            
            $0.contentInset = UIEdgeInsets(top: self.headerHeight, left: 0, bottom: 0, right: 0)
            $0.contentOffset = CGPoint(x: 0, y: -self.headerHeight)
            
        }
        self.view.addSubview(self.infoTableView)
        self.infoTableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
    }
}

// MARK: - UITableViewDataSource
extension SourceSubViewController : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let goodsList = self.infoData?["goodsList"] as? [JSONObject] {
            return goodsList.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: BookCell.reuseId) as! BookCell
        if let goodsList = self.infoData?["goodsList"] as? [JSONObject], goodsList.count > indexPath.row {
            cell.refresh(data: goodsList[indexPath.row], isShowSourceType: true)
        } else {
            cell.refresh(data: nil)
        }
        return cell
    }
    
}

// MARK: - UITableViewDelegate
extension SourceSubViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  BookCell.height
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let goodsList = self.infoData?["goodsList"] as? [JSONObject], goodsList.count > indexPath.row {
            let itemData = goodsList[indexPath.row]
            
            if let sourceType = SourceType(type: itemData["type"] as? Int),
                let goods_id = itemData["goods_id"] as? Int {
                
                if sourceType == .电子书 {
                    let controller = EBookDetailViewController(goods_id: goods_id)
                    self.show(controller, sender: nil)
                } else {
                    let controller = SourceDetailViewController(sourceType: sourceType, goods_id: goods_id)
                    self.show(controller, sender: nil)
                }
                
            }
            
        }
    }
}

// MARK: - UIScrollViewDelegate
extension SourceSubViewController : UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.scrollHandle?(self.index,scrollView)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            self.endDecelerating?(self.index,scrollView)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.endDecelerating?(self.index,scrollView)
    }
}
